package cn.dylan.transation;

import org.apache.rocketmq.client.producer.LocalTransactionState;
import org.apache.rocketmq.client.producer.TransactionListener;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;

public class MyTransationListener implements TransactionListener {
    @Override
    public LocalTransactionState executeLocalTransaction(Message message, Object o) {
        // 执行业务，步骤3.4
        try {
            System.out.println("业务执行了");
            // 成功后提交
            return LocalTransactionState.COMMIT_MESSAGE;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("提交失败");
            return LocalTransactionState.ROLLBACK_MESSAGE;
        }
    }

    // 步骤5.6.7 本地事务查询
    @Override
    public LocalTransactionState checkLocalTransaction(MessageExt messageExt) {
        try {
            System.out.println("查询到提交了");
            return LocalTransactionState.COMMIT_MESSAGE;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("查询到未提交，因为"+e.getMessage());
            return LocalTransactionState.ROLLBACK_MESSAGE;
        }
    }
}
